﻿using DeployTool.Core;
using System;
using System.IO;
using System.Windows.Forms;

namespace DeployTool.CustomControl
{
    public partial class ProgramUserControl : UserControl, IDeployStep
    {
        readonly string _rootDir;
        readonly string _programPath;
        public ProgramUserControl()
        {
            InitializeComponent();
            _rootDir = Tool.GetRootDir();
            _programPath = Path.Combine(_rootDir, "wlwsys_intranet/wlwsys.web.intranet.exe");
        }

        #region IDeployStep
        public StepEnum Step { get; set; } = StepEnum.Third;

        public bool CanNextStep()
        {
            return true;
        }

        public void RefreshData()
        {
        }
        #endregion

        #region private methods

        /// <summary>
        /// 启动程序
        /// </summary>
        private void StartProgram()
        {
            int.TryParse(txt_port.Text.Trim(), out int port);
            string args = $"--urls=http://*:{port}{(checkBox_https.CheckState == CheckState.Checked ? ";https://*:443" : "")}";
            string cmd = _programPath;
            if (checkBox_window.CheckState == CheckState.Checked)
            {
                cmd = "mshta";
                args = $"vbscript:CreateObject(\"Wscript.Shell\").Run(\"{_programPath} {args}\",0,true)(window.close)";
            }

            Tool.StartProgramWithNoResult(cmd, args);
            if (checkBox_window.CheckState == CheckState.Checked)
            {
                Tool.KillProgramByName("mshta");
            }
        }

        private void btn_Click(object sender, EventArgs e)
        {
            StartProgram();
        }
        #endregion
    }
}
